package guzb.cnblogs.security.industrydemo.auth;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Sqlite3数据的查询操作
 */
@Mapper
public interface SecurityMapper {

    @Select({
            "SELECT id, username, password, name, mobile_phone,",
            "is_enabled, is_locked, account_expiry_time, password_last_update_time",
            "FROM user WHERE username = #{username}"
    })
    UserEntity getByUsername(@Param("username") String username);

    @Select({
            "SELECT name, title, type",
            "FROM authority WHERE name IN",
            "(",
            "  SELECT authority_name",
            "  FROM user_authority_relation",
            "  WHERE user_id=#{userId}",
            ")"
    })
    List<AuthorityEntity> listAuthoritiesOfUser(@Param("userId") Integer userId);


    @Select({
            "select (a.type || '_' || a.name) as authority, a.title as resource_title, ",
            "h.uri as resource_uri, h.http_method as resource_http_method",
            "from http_resource_authority_relation r",
            "left join http_resource h on r.http_resource_id = h.id",
            "left join authority a on r.authority_name = a.name"
    })
    List<AuthorityResourceItem> listAllAuthorityResources();
}
